using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Microsoft.Practices.EnterpriseLibrary.Data;
using System.Data.Common;
using Instedd.Labs.Geo.Library.Kml;

namespace Instedd.Labs.GeoBlogging.WebApplication
{
    public partial class CreatePost : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (this.IsPostBack)
            {
                string decimalLatString = Request.Form["edDecimalLat"];
                string decimalLongString = Request.Form["edDecimalLong"];

                if (String.IsNullOrEmpty(decimalLatString))
                {
                    decimalLatString = "0";
                }

                if (String.IsNullOrEmpty(decimalLongString))
                {
                    decimalLongString = "0";
                }

                LatLong latLong = null;
                try
                {
                    //latLong = new LatLong(decimalLatString, decimalLongString);
                    latLong = LatLong.Parse(decimalLatString, decimalLongString);
                }
                catch (ArgumentOutOfRangeException ex)
                {
                    return;
                }

                if (cxRememberMe.Checked)
                {
                    Response.Cookies.Add(new HttpCookie("rememberMe", "1"));
                    Response.Cookies.Add(new HttpCookie("postCreator", edFrom.Text));
                    Response.Cookies.Add(new HttpCookie("lat", decimalLatString));
                    Response.Cookies.Add(new HttpCookie("long", decimalLongString));
                }
                else
                {
                    Response.Cookies.Add(new HttpCookie("rememberMe", "0"));
                    Response.Cookies.Add(new HttpCookie("postCreator", ""));
                    Response.Cookies.Add(new HttpCookie("lat", ""));
                    Response.Cookies.Add(new HttpCookie("long", ""));
                }

                DataAccess.AddRssEntry(Guid.NewGuid(), edFrom.Text, edTitle.Text, edBody.Text, "", Guid.NewGuid(), DateTime.Now, latLong);
                Response.Redirect("/");
            }
            else
            {
                if (Request.Cookies["rememberMe"] != null)
                {
                   if(Request.Cookies["rememberMe"].Value != null)
                   {
                            cxRememberMe.Checked = true;
                   }
                }
                else
                {
                    cxRememberMe.Checked = false;
                }
                if (Request.Cookies["postCreator"] != null)
                {
                    edFrom.Text = Request.Cookies["postCreator"].Value;
                }
                if (Request.Cookies["lat"] != null)
                {
                    String lat = Request.Cookies["lat"].Value.ToString();
                    if (String.IsNullOrEmpty(lat))
                    {
                        lat = "0";
                    }
                    ClientScript.RegisterStartupScript(typeof(CreatePost), "setLat", "document.getElementById('edDecimalLat').value=" + lat +";", true);
                }
                if (Request.Cookies["long"] != null)
                {
                    String lon = Request.Cookies["long"].Value.ToString();
                    if (String.IsNullOrEmpty(lon))
                    {
                        lon = "0";
                    }
                    ClientScript.RegisterStartupScript(typeof(CreatePost), "setLong", "document.getElementById('edDecimalLong').value=" + lon + ";", true);
                }

            }
        }
    }
}
